class_name UnitSprite extends Sprite2D

@export var component_type_list : Array[Enums.COMPONENT_TYPE] = [Enums.COMPONENT_TYPE.UNIT_SPRITE]
#开始时是否随机旋转一个角度
@export var rotate_random_enable : bool = false
@export var shake_strength : float:
	set = _set_shake_strength
@export var hurt_enable : bool:
	set = _set_hurt_enable
@export var wave_strength_scale : float:
	set = _set_wave_strength_scale
@export var wave_shake : bool:
	set = _set_wave_shake
@export var wave_enable : bool:
	set = _set_wave_enable
@export var out_line_enable : bool:
	set = _set_out_line_enable
@export var water_level:float:
	set = _set_water_level
@onready var hurt_animation_player : AnimationPlayer = $HurtAnimationPlayer
@onready var shake_animation_player : AnimationPlayer = $ShakeAnimationPlayer

func _ready():
	material.set_shader_parameter("h_frames", hframes)
	material.set_shader_parameter("v_frames", vframes)
	if rotate_random_enable:
		rotation_degrees = randf_range(0, 360)

func _process(_delta):
	material.set_shader_parameter("frames", frame)

func play_shake_effect(direct : float):
	if direct > 0:
		shake_animation_player.play("shake_left")
	else:
		shake_animation_player.play("shake_right")

func play_hurt_effect():
	hurt_animation_player.play("active") 
	
func _set_water_level(val:float):
	material.set_shader_parameter("water_body_level", val)

func _set_shake_strength(value : float):
	material.set_shader_parameter("wave_out_time", value)

func _set_hurt_enable(value : bool):
	material.set_shader_parameter("hurt_enable", value)

func _set_wave_strength_scale(value : float):
	material.set_shader_parameter("wave_strength_scale", value)

func _set_wave_shake(value : bool):
	material.set_shader_parameter("wave_shake", value)

func _set_wave_enable(value : bool):
	material.set_shader_parameter("wave_active", value)

func _set_out_line_enable(value : bool):
	material.set_shader_parameter("out_line_enable", value)

func _init_data(data : Dictionary):
	if data.has("body_sprite"):
		texture = load(data['body_sprite'])
	scale.x = data.get('body_sprite_size_x', scale.x)
	scale.y = data.get("body_sprite_size_y", scale.y)
